<?php
add_action('widgets_init', 'register_lastest_coupon_widget');

function register_lastest_coupon_widget() {
    register_widget('lastest_coupon');
}

class lastest_coupon extends WP_Widget // widget class 
{

    function lastest_coupon() { // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest coupon-w', 'description' =>
            'Display top lastest coupons - Home page or Store detail page');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'lastest_coupon');
        $this->WP_Widget('lastest_coupon', 'CouponX - Lastest coupons', $widget_ops, $control_ops);
    }

    function widget($args, $instance) { // display widget
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);
        $kw_after_title = $instance['kw_after_title'];
        $itemsToShow = $instance['number_show'];
        if (!$itemsToShow)
            $itemsToShow = 10;
        $coupon_type = $instance['type_of_coupon'];
        $show_at = $instance['show_at'];

        if ($show_at == 'home') // Show widget at homepage
            include('inc/lastest_cp_home.php');
        else if ($show_at == 'store_detail') // Show widget at store detail page
        {
            global $post;
            if ($post)
                $store_id = $post->ID;            
            if ($store_id)
            {
                include('inc/lastest_cp_store_detail.php');
            }
                
        }
    }

    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['number_show'] = $new_instance['number_show'];
        $instance['type_of_coupon'] = $new_instance['type_of_coupon'];
        $instance['show_at'] = $new_instance['show_at'];
        $instance['kw_after_title'] = $new_instance['kw_after_title'];
        return $instance;
    }

    function form($instance) { // form for the widget options
        $arr_type_of_coupon = array('Coupon' => 'code', 'Promotion' => 'promotion', 'Free Shipping' => 'free_shipping');
        $arr_showat = array('home', 'store_detail')
        ?>
        <div style="color: #333;">
            <p>        
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id('kw_after_title'); ?>"><?php echo 'Keyword after store name'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('kw_after_title'); ?>" name="<?php echo $this->get_field_name('kw_after_title'); ?>" value="<?php echo $instance['kw_after_title']; ?>" style="width:60%;" placeholder="ex: coupons, etc.." />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id('number_show'); ?>"><?php echo 'Number of coupons to show (default 10)'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('number_show'); ?>" name="<?php echo $this->get_field_name('number_show'); ?>" value="<?php echo $instance['number_show']; ?>" style="width:60%;" />
            </p>
            <p id="sl_cp_type">
                <label for="<?php echo $this->get_field_id('type_of_coupon'); ?>"><?php _e('Coupon type (if display at Home page)', ''); ?></label>
                <select name="<?php echo $this->get_field_name('type_of_coupon'); ?>" id="<?php echo $this->get_field_id('type_of_coupon') ?>" style="width: 60%;">
        <?php
        foreach ($arr_type_of_coupon as $k => $v) {
            if ($instance['type_of_coupon'] == $v)
                $selected = 'selected';
            else
                $selected = '';
            echo "<option value='{$v}' {$selected}>$k</option>";
        }
        ?>
                </select>    
            </p>
            <p>
                <label for="<?php echo $this->get_field_id('show_at'); ?>"><?php _e('Show at', ''); ?></label>
                <select name="<?php echo $this->get_field_name('show_at'); ?>" id="<?php echo $this->get_field_id('show_at') ?>" style="width: 60%;">
        <?php
        foreach ($arr_showat as $v) {
            if ($instance['show_at'] == $v)
                $selected = 'selected';
            else
                $selected = '';
            echo "<option value='{$v}' {$selected}>$v</option>";
        }
        ?>
                </select>
            </p>	     
        </div>
                    <?php
                }

            }
            ?>
